package _树;

import sun.util.resources.cldr.xh.CurrencyNames_xh;

import java.util.ArrayList;
import java.util.Stack;

/**
 * 173： BST的迭代器：https://leetcode.cn/problems/binary-search-tree-iterator/
 */
class BSTIterator {

    ArrayList<Integer> arr = new ArrayList<>();
    int x = 0;  //游标

    public BSTIterator(TreeNode root) {
        getInOrderArray(root);
    }

    private void getInOrderArray(TreeNode root) {
        Stack<TreeNode> st = new Stack<>();
        TreeNode cur = root;
        while (!st.isEmpty() || cur != null) {
            if (cur != null) {
                st.push(cur);
                cur = cur.left;
            } else {
                cur = st.pop();
                arr.add(cur.val);
                cur = cur.right;
            }
        }
    }

    public int next() {
        return arr.get(x++);
    }

    public boolean hasNext() {
        return x < arr.size();
    }
}